package pers.jason.wppm.workflow.listener;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.util.StringUtils;
import pers.jason.wppm.workflow.exception.ActivitiException;

import java.util.Arrays;

/**
 * @Author: JZH
 * @Date: 2018/11/14 21:56
 * @Description:
 */
public class EformTaskListener implements TaskListener {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 动态设置审批人
     * @param delegateTask
     */
    @Override
    public void notify(DelegateTask delegateTask) {
        String taskName = delegateTask.getName();
        logger.info("进入监听器，创建的任务名：" + taskName);
        if("需求提出".equals(taskName)) { //第一个节点
            delegateTask.setAssignee("003571");
        } else {
            String processInstanceId = delegateTask.getProcessInstanceId();
            Object objParams = delegateTask.getVariable(processInstanceId+":"+taskName);
            if(null == objParams) {
                logger.debug("发生异常：审批人为空");
                throw new ActivitiException("没有指定合法审批人");
            }
            String auditors = objParams.toString(); //获取审批人
            if(auditors.indexOf(";") > -1) { //大于一个人
                delegateTask.addCandidateUsers(Arrays.asList(auditors.split(";")));
            } else { //一个人
                delegateTask.setAssignee(auditors);
            }
        }

    }
}
